﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Data.OleDb;

namespace ColorBall
{
    public static class OpenData
    {
         

        /// <summary>
        /// 读取excel指定页中的内容
        /// </summary>
        /// <param name="strSheetName">页名</param>
        /// <param name="strExcelFileName">excel路径</param>
        /// <returns></returns>
        public static DataTable GetDataTable(string strSheetName, string strExcelFileName)
        {
            //源的定义
            string strConn = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source={0};" + "Extended Properties='Excel 8.0;HDR=NO;IMEX=1';", strExcelFileName);

            //Sql语句
            string strExcel = string.Format("select * from [{0}$]", strSheetName);

            //定义存放的数据表
            DataSet ds = new DataSet();

            //连接数据源
            OleDbConnection conn = new OleDbConnection(strConn);

            try
            {
                conn.Open();
                //适配到数据源
                OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
                adapter.Fill(ds, strSheetName);
            }
            catch (Exception e)
            {
                throw e;
            }
            finally
            {
                conn.Close();
            }
            return ds.Tables[strSheetName];
        }

    }
}
